<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/10/10 15:14
 * Desc:
 */
class Service_Api_News extends Contract_Api_News
{
    /**
     * 资讯类型
     */
    const TYPE_ID = 115;

    public function get_attr(): array
    {
        $data = DB::select('attrname', 'id')
            ->from('news_attr')
            ->where('isopen', '=', 1)
            ->and_where('pid', '=', 0)
            ->order_by('displayorder', 'asc')
            ->execute()
            ->as_array();
        array_unshift($data, array('attrname' => '推荐', 'id' => "0"));

        return $data;
    }

    public function get_list($params): array
    {
        $page     = Arr::get($params, 'page', 1);
        $pagesize = Arr::get($params, 'pagesize', 10);
        $attr_id  = Arr::get($params, 'attr_id', 0);

        //重写查询
        $offset = St_Page::offset($page, $pagesize);

        $builder = DB::select('a.title', 'a.id', 'a.piclist', 'a.litpic', 'a.shownum', 'a.modtime', 'a.ishidden', 'b.displayorder')
            ->from(array('news', 'a'))
            ->join(array('allorderlist', 'b'), 'left')
            ->on('a.id', '=', DB::expr('sline_b.aid'))
            ->on('b.typeid', '=', DB::expr(self::TYPE_ID))
            ->where('a.ishidden', '=', 0);

        if ($attr_id && $attr_id != 0) {
            $builder->and_where('a.category_one', '=', $attr_id);
        }

        $builder->order_by(DB::expr("IFNULL(sline_b.displayorder,9999) asc,sline_a.modtime desc,sline_a.addtime DESC"));

        $list = $builder->limit($pagesize)->offset($offset)->execute()->as_array();

        foreach ($list as &$item) {
            $item['litpic']  = $this->img_url($item['litpic']);
            $piclist         = explode(',', $item['piclist']);
            $item['piclist'] = array();
            foreach ($piclist as $k => $item1) {
                $item['piclist'][] = $this->img_url($item1, 220, 160);
                if ($k >= 2) {
                    break;
                }
            }
            $item['modtime'] = date('Y-m-d', $item['modtime']);
        }

        return $list;
    }

    public function get_detail($params): array
    {
        //资讯id
        $id = $params['id'];

        $detail = DB::select(
            'title', 'comefrom', 'fromsite', 'author', 'content', 'modtime',
            'shownum', 'description', 'category_two', 'tagword', 'modtime'
        )
            ->from('news')
            ->where('id', '=', $id)
            ->where('ishidden', '=', 0)
            ->execute()
            ->current();

        if (empty($detail)) {
            throw new Exception('查询失败');
        }

        // 点击率
        DB::update('news')
            ->set(array('shownum' => $detail['shownum'] + 1))
            ->where('id', '=', $id)
            ->execute();

        //相关资讯
        $detail['relevantNews'] = DB::select('n.title', 'n.id', 'n.litpic', 'n.shownum', 'n.addtime')
            ->from(array('news', 'n'))
            ->join(array('allorderlist', 'a'))
            ->on('n.id', '=', DB::expr('sline_a.aid'))
            ->where('n.ishidden', '=', 0)
            ->and_where('n.category_two', '=', $detail['category_two'])
            ->and_where('n.id', '!=', $id)
            ->group_by('n.id')
            ->order_by(DB::expr('IFNULL(sline_a.displayorder,9999) ASC'))
            ->offset(0)
            ->limit(4)
            ->execute()
            ->as_array();

        foreach ($detail['relevantNews'] as &$item) {
            $item['litpic']  = $this->img_url($item['litpic']);
            $item['addtime'] = date('Y-m-d', $item['addtime']);
        }

        $detail['modtime'] = date('Y-m-d', $detail['modtime']);
        $detail['content'] = $this->content_add_domain($detail['content']);

        $recommend = array();
        if (St_Functions::is_normal_app_install('product_seeding')) {
            //今日推荐
            $seed = DB::select()
                ->from('product_seeding')
                ->where('id', '=', 2)
                ->and_where('status', '=', 1)
                ->execute()
                ->current();

            if ($seed && $seed['typeid']) {
                $detail['recommend_type_id'] = $seed['typeid'];
                $recommend = Model_Api_V2_Product_Seeding::recommend($seed, $detail['tagword']);
                foreach ($recommend as &$item) {
                    $item['litpic'] = $this->img_url($item['litpic'], 340, 180);
                }
            }
        }
        $detail['recommend'] = $recommend;

        return $detail;
    }
}